<?php

namespace app\admin\controller;

use think\Controller;

class Admin extends Controller
{
    //管理员列表
    public function all(){
        $admins = model('Admin')->order('status', 'desc')->paginate(10);
        $this->assign('admins', $admins);
//        session('index', null);
        return view();
    }

    //状态改变
    public function status(){
        $data = [
            'id' => input('post.id'),
            'status' => input('post.status')
        ];
        $result = model("Admin")->status($data);
        if ($result == 1){
            $this->success('操作成功', 'admin/admin/all');
        }else{
            $this->error($result);
        }
    }

    //管理员添加
    public function add(){
        if (request()->isAjax()){
            $data = [
                'username' => input('post.username'),
                'password' => input('post.password'),
                'conpass' => input('post.conpass'),
                'nickname' => input('post.nickname'),
                'email' => input('post.email')
            ];
            $result = model('Admin')->add($data);
            if ($result == 1){
                $this->success('添加成功', 'admin/admin/all');
            }else{
                $this->error($result);
            }
        }
        return view();
    }

    //管理员编辑
    public function edit(){
        $id = input('id');
        if (request()->isAjax()){
            $data = [
                'id' => $id,
                'nickname' => input('post.nickname'),
                'oldpass' => input('post.oldpass'),
                'password' => input('post.password'),
                'conpass' => input('post.conpass')
            ];
            $result = model('Admin')->edit($data);
            if ($result == 1){
                $this->success('修改成功', 'admin/admin/all');
            }else{
                $this->error($result);
            }
        }
        $admins = model('Admin')->find($id);
        $this->assign('admins', $admins);
        return view();
    }
}
